#include <dbg.h>
#include <gtest/gtest.h>
using namespace std;

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        if (root == nullptr) {
            return {};
        }

        vector<vector<int>> res;
        queue<TreeNode*> que;
        que.push(root);
        bool isToLeft = false;
        while (!que.empty()) {
            vector<int> vals;
            for (int i = que.size(); i > 0; --i) {
                TreeNode* node = que.front();
                que.pop();
                vals.emplace_back(node->val);
                if (node->left != nullptr) {
                    que.push(node->left);
                }
                if (node->right != nullptr) {
                    que.push(node->right);
                }
            }
            if (isToLeft) {
                reverse(vals.begin(), vals.end());
            }
            res.emplace_back(std::move(vals));
            isToLeft = !isToLeft;
        }
        return res;
    }
};

TEST(TEST1, zigzagLevelOrder) {
    Solution s;

    TreeNode* root = new TreeNode(3);
    root->left = new TreeNode(9);
    root->right = new TreeNode(20);
    root->right->left = new TreeNode(15);
    root->right->right = new TreeNode(7);

    vector<vector<int>> res{{3}, {20, 9}, {15, 7}};
    EXPECT_EQ(s.zigzagLevelOrder(root), res);
}

TEST(TEST2, zigzagLevelOrder) {
    Solution s;

    TreeNode* root = new TreeNode(1);

    vector<vector<int>> res{{1}};
    EXPECT_EQ(s.zigzagLevelOrder(root), res);
}

int main(int argc, char* argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}